package edu.fdzc.big_event.controller;


import edu.fdzc.big_event.pojo.Result;
import edu.fdzc.big_event.pojo.User;
import edu.fdzc.big_event.service.UserService;
import edu.fdzc.big_event.utils.JwtUtil;
import edu.fdzc.big_event.utils.Md5Util;
import edu.fdzc.big_event.utils.ThreadLocalUtil;
import jakarta.validation.constraints.Pattern;
import org.hibernate.validator.constraints.URL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
@Validated
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @PostMapping("/register")
    public Result register(@RequestParam @Pattern(regexp = "^\\S{5,16}$") String username, @RequestParam @Pattern(regexp = "^\\S{5,16}$") String password) {

        User user = userService.findByUserName(username);
        if(user == null) {
            userService.register(username,password);
            return Result.success("注册成功");
        }
        return Result.error("用户名重复");
    }
    @PostMapping("/login")
    public Result<String> login(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "^\\S{5,16}$") String password) {
        System.out.println(username);
        User user = userService.findByUserName(username);
        if(user == null) {
            return Result.error("用户名错误");
        }
        if(Md5Util.getMD5String(password).equals(user.getPassword())) {
            Map<String , Object> claims = new HashMap<>();
            claims.put("id", user.getId());
            claims.put("username", user.getUsername());
            String thoken = JwtUtil.genToken(claims);
            ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
            operations.set(thoken, thoken,1, TimeUnit.HOURS);
            return Result.success(thoken);
        }
        return Result.error("密码错误");
    }

    @GetMapping("/userinfo")
    public Result<User> userInfo() {
        Map<String,Object> claims = ThreadLocalUtil.get();
        String username = (String) claims.get("username");
        User user = userService.findByUserName(username);
        return Result.success(user);
    }

    @PutMapping("/update")
    public Result update(@RequestBody @Validated User user) {
        userService.updateUser(user);
        return Result.success("更新完成");
    }

    @PatchMapping("updateAvter")
    public Result updateavterUrl(@RequestParam @URL String avterUrl ) {
        userService.updateAvter(avterUrl);
        return Result.success();
    }

    @PatchMapping("/updatePwd")
    public Result updatePsw(@RequestBody Map<String,String> params,@RequestHeader("Authorization") String thoken){
        //参数检验
        String oldPwd = params.get("oldPwd");
        String newPwd = params.get("newPwd");
        String reNewPwd = params.get("reNewPwd");
        if(!StringUtils.hasLength(oldPwd) || !StringUtils.hasLength(newPwd) || !StringUtils.hasLength(reNewPwd)) {
            return Result.error("缺少必要参数");
        }
        Map<String,Object> claims = ThreadLocalUtil.get();
        String username = (String) claims.get("username");
        User user = userService.findByUserName(username);
        if(!user.getPassword().equals(Md5Util.getMD5String(oldPwd))) {
            return Result.error("原密码填写不正确");
        }

        if(!reNewPwd.equals(newPwd)) {
            return Result.error("两次密码不一样");
        }
        userService.updatePwd(newPwd);
        //删除以前的thoken
        ValueOperations<String, String> stringStringValueOperations = stringRedisTemplate.opsForValue();
        stringStringValueOperations.getOperations().delete(thoken);
        return Result.success();
    }

}
